<template>
	<!--  委外-->
	<div class="content">
		<div class="pageSearchView">
			<el-form :inline="true" :model="searchParam" class="demo-form-inline">
				<el-form-item label="物料" style="width: 250px" prop="materalId">
					<el-select v-model="searchParam.materialId" placeholder="请选择物料" filterable remote
										 :remote-method="getMaterialList">
						<el-option v-for="(item, index) of materialList" :key="index" :label="item.materialName" :value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="委外单据号">
					<el-input v-model="searchParam.outsourcingNo" placeholder="请输入委外单据号" clearable style="width: 200px" />
				</el-form-item>
				<el-form-item label="供应商">
					<el-input v-model="searchParam.supplierName" placeholder="请输入供应商名称" clearable style="width: 200px" />
				</el-form-item>

				<!--				<el-form-item label="仓库名称">-->
				<!--					<el-tree-select v-model="searchParam.warehouseId" :data="wareList" check-strictly clearable filterable-->
				<!--													:render-after-expand="false" :props="{-->
				<!--              children: 'sonList',-->
				<!--              label: 'warehouseName',-->
				<!--              value: 'id',-->
				<!--            }" style="width: 205px" @change="wareChange" />-->
				<!--				</el-form-item>-->
				<!--				<el-form-item label="物料状态" style="width: 250px" prop="materalId">-->
				<!--					<el-select v-model="searchParam.materialStatusList" placeholder="请选择" clearable collapse-tags-->
				<!--										 collapse-tags-tooltip filterable multiple style="width: 205px">-->
				<!--						<el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />-->
				<!--					</el-select>-->
				<!--				</el-form-item>-->

				<!--				<el-form-item label="库位名称">-->
				<!--					<el-select v-model="searchParam.storageId" placeholder="请选择" clearable filterable style="width: 205px">-->
				<!--						<el-option v-for="item in storageList" :key="item.id" :label="item.storageName" :value="item.id" />-->
				<!--					</el-select>-->
				<!--				</el-form-item>-->
				<!--				<el-form-item label="加工属性">-->
				<!--					<el-select v-model="searchParam.processingStatus" placeholder="请选择加工属性" clearable filterable-->
				<!--										 style="width: 160px">-->
				<!--						<el-option :key="0" label="未加工" :value="0" />-->
				<!--						<el-option :key="1" label="已加工" :value="1" />-->
				<!--					</el-select>-->
				<!--				</el-form-item>-->
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
					<el-button :icon="Refresh" @click="resetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="pageTableView">
			<div class="pageTableMenuView" style="width: 100%">
				<el-tabs v-model="activeTab" @tab-click="handleTabClick" style="width: 100%">
					<el-tab-pane label="出库" name="outbound"></el-tab-pane>
					<el-tab-pane label="入库" name="inbound"></el-tab-pane>
				</el-tabs>
			</div>
			<!-- 表格 -->
			<el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 320px)">
				<el-table-column label="序号" align="center" width="70">
					<template #default="scope">
						{{ (pageSearch.current - 1) * pageSearch.pageSize + scope.$index + 1 }}
					</template>
				</el-table-column>

				<el-table-column prop="outsourcingNo" label="委外单据号" align="center" min-width="200" show-overflow-tooltip />
				<el-table-column prop="materialNo" label="物料" align="center" min-width="200" show-overflow-tooltip />
				<!--				<el-table-column prop="materialName" label="物料名称" align="center" min-width="200" show-overflow-tooltip  />-->

				<el-table-column prop="materialBatchNo" label="库位物料批次号" align="center" min-width="200" show-overflow-tooltip />
				<el-table-column prop="planCount" label="计划数量" align="center" min-width="120" />
				<el-table-column prop="outCount" label="委外出库数量" align="center" min-width="120" />
				<el-table-column prop="intCount" label="委外入库数量" align="center" min-width="120" />
				<el-table-column prop="supplierName" label="供应商" align="center" min-width="200" show-overflow-tooltip />
				<el-table-column prop="traceabilityCodePlace" label="追溯码位数取值" align="center" min-width="180" />
				<el-table-column prop="description" label="描述" align="center" min-width="120" show-overflow-tooltip />
<!--				<el-table-column label="是否启用" align="center" min-width="120">-->
<!--					<template #default="scope">-->
<!--						{{ scope.row.activate ? '启用' : '未启用' }}-->
<!--					</template>-->
<!--				</el-table-column>-->
<!--				<el-table-column label="是否逻辑删除" align="center" min-width="120">-->
<!--					<template #default="scope">-->
<!--						{{ scope.row.isDeleted ? '已删除' : '未删除' }}-->
<!--					</template>-->
<!--				</el-table-column>-->
				<!-- 排除所有名为 'id' 的字段 -->
				<!-- ... 其他字段 ... -->
			</el-table>

			<div class="pagePagination">
				<el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
											 :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next"
											 :total="pageTotal"
											 @size-change="handleSizeChange" @current-change="handleCurrentChange" />
			</div>
		</div>
	</div>
</template>
<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue';

onMounted(() => {
	getList();
	getOption();
});


const pageSearch = ref({
	current: 1,
	pageSize: 10,
	inventoryStatus: 'out'
});

const formatStatus = (row, column, cellValue) => {
	switch (cellValue) {
		case 0:
			return '不可用';
		case 1:
			return '可用';
		case 2:
			return '运输中';
		case 3:
			return '生产锁定';
		case 4:
			return '不合格';
		case 5:
			return '报废';
	}
};

const activeTab = ref('outbound');

const handleTabClick = (tab) => {
	console.log(tab.props.name, 'clicked');
	if (tab.props.name === 'outbound') {
		pageSearch.value.inventoryStatus = 'out';
	} else if (tab.props.name === 'inbound') {
		pageSearch.value.inventoryStatus = 'in';
	}
	// 切换标签页后重新获取列表
	getList();
};

const formatProcessingAttributes = (row, column, cellValue) => {
	switch (cellValue) {
		case 0:
			return '未加工';
		case 1:
			return '已加工';

	}
};

const workOrderList = ref({});

const materialList = ref([]); // 物料表

const pageTotal = ref(null);//页面总数
const searchParam = ref({});//页面检索条件


// 判断质检类型
const getQualityTypeByValue = (e) => {
	let name = null;
	for (const item of qualityTypeList) {
		if (e == item.value) {
			name = item.label;
		}
	}
	return name;
};

// 质检类型
const qualityTypeList = reactive([
	{
		label: '部分送货',
		value: '1'
	},
	{
		label: '全部送货',
		value: '2'
	},
	{
		label: '未送货',
		value: '0'
	}
	// 继续添加更多检查类型...
]);


const paginatedData = ref([]);
const materiaList = ref([]);//物料表
const wareList = ref([]);//仓库
const storageList = ref({});
const procedureOrderList = ref([]);//工序订单
const supplierList = ref([]);
const storageAllList = ref([]);

const statusList = ref([{
	label: '不可用',
	value: 0
}, {
	label: '可用',
	value: 1
}, {
	label: '运输中',
	value: 2
}, {
	label: '生产锁定',
	value: 3
}, {
	label: '不合格',
	value: 4
}, {
	label: '报废',
	value: 5
}]);


const getMaterialList = (e) => {
	if (e) {
		axiosInstance.post('/api/basedataMaterial/getPage', {
			current: 1,
			pageSize: 20,
			materialName: e
		}).then(res => {
			materialList.value = res.data.records;
		});
	}
};

// 供应商远端搜索
const supplierSearch = async (query: string) => {
	if (query) {
		try {
			const response = await axiosInstance.post(`/api/basedataSupplier/getPage`, {
				current: 1,
				pageSize: 20,
				name: query
			});
			supplierList.value = await response.data.records; // 假设返回的数据直接是供应商列表
		} catch (error) {
			supplierList.value = []; // 出错时清空列表
		}
	}
};
const deviceList = ref([]);
const wareChange = (e, type) => {
	// storageList.value = {}
	axiosInstance.post('/api/basedataStorage/listByPojoPage', {
		warehouseId: e,
		current: 1,
		pageSize: 999999
	})
		.then((res) => {
			if (res.code !== 200) {
				ElMessage.warning(res.message);
				return false;
			}
			storageList.value = res.data.records;
		});
};


const getOption = () => {
	axiosInstance.post('/api/basedataMaterial/getPage', {
		current: 1,
		pageSize: 20
	})
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			materialList.value = res.data.records;
		});
	// axiosInstance.post('/api/basedataEquipment/getAll', {})
	//  .then((res) => {
	//   if (res.code !== 200) {
	//    ElMessage.warning(res.message);
	//    return false;
	//   }
	//   deviceList.value = res.data;
	//  });

	// axiosInstance.post('/api/outsourcingPlan/querySourcingPlanProcedureOrder', {})
	//  .then(res => {
	//   workOrderList.value = res.data;
	//  });

	// // 查询所有库位
	// axiosInstance.post('/api/basedataStorage/getAll', {})
	//  .then(res => {
	//   storageAllList.value = res.data;
	//  });


	// axiosInstance.post(`/api/basedataSupplier/getPage`, {
	//  current: 1,
	//  pageSize: 20
	// }).then(res => {
	//  if (res.code == 200) {
	//   supplierList.value = res.data.records;
	//  }
	// });


	// axiosInstance.post('/api/planProcedureOrder/getAll', {})
	//  .then(res => {
	//   if (res.code !== 200) {
	//    ElMessage.error(res.message);
	//    return false;
	//   }
	//   const list = res.data;
	//   const loop = [];
	//   for (const item of list) {
	//    loop.push(
	//     {
	//      value: item.id,
	//      label: item.materialName
	//     }
	//    );
	//   }
	//   procedureOrderList.value = list;
	//  });

	axiosInstance.get('/api/basedataWarehouse/treeList', {}).then(res => {
		if (res.code !== 200) {
			ElMessage.error(res.message);
			return false;
		}
		wareList.value = res.data;
	});
};

// 获取列表接口
const getList = () => {
	axiosInstance.post('/api/outsourcingPlan/getReportPage', {
			...searchParam.value,
			...pageSearch.value
		}
	)
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			// const list = res.data.records;
			// for (const item of list) {
			//  item.children = item.outsourcingWorkMaterialVOS;
			// }
			paginatedData.value = res.data.records;
			pageTotal.value = res.data.total;
		});
};

const handleSizeChange = (e) => {
	pageSearch.value.pageSize = e;
	getList();
};

const handleCurrentChange = (e) => {
	pageSearch.value.current = e;
	getList();
};

// 重置搜索条件
const resetSearch = () => {
	searchParam.value = {};
	pageSearch.value.current = 1;
	// current.value = 1
	getList();
};

</script>
<style scoped></style>
